package com.aisile.sellergoods.service.impl;

import com.aisile.entity.PageResult;
import com.aisile.mapper.TbSpecificationMapper;
import com.aisile.mapper.TbSpecificationOptionMapper;
import com.aisile.pojo.TbSpecification;
import com.aisile.pojo.TbSpecificationExample;
import com.aisile.pojo.TbSpecificationOption;
import com.aisile.pojo.TbSpecificationOptionExample;
import com.aisile.pojogroup.Specification;
import com.aisile.sellergoods.service.SpecificationService;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @标题 SpecificationServiceImpl
 * @作者 Mr.chen
 * @创建日期 2018/12/28 10:40
 * @版本号 1.0
 **/
@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public PageResult findPage(int page, int rows, TbSpecification tbSpecification) {
        PageHelper.startPage(page,rows);
        String specName = tbSpecification.getSpecName();
        TbSpecificationExample example = new TbSpecificationExample();
        if(!StringUtils.isEmpty(specName)){
            example.createCriteria().andSpecNameLike("%"+specName+"%");
        }
        Page<TbSpecification> pageModel = (Page<TbSpecification>) tbSpecificationMapper.selectByExample(example);
        return new PageResult(pageModel.getTotal(),pageModel.getResult());
    }

    @Override
    public void save(Specification specification) {
        TbSpecification spec = specification.getSpec();//规格主表数据
        //规格 选项列表数据
        List<TbSpecificationOption> specOptionList = specification.getSpecOptionList();
        //保存主表信息
        tbSpecificationMapper.insert(spec);
        //保存选项信息
        if(specOptionList!=null&&specOptionList.size()>0){
            for(TbSpecificationOption option : specOptionList){
                option.setSpecId(spec.getId());
                tbSpecificationOptionMapper.insert(option);
            }
        }
    }

    @Override
    public Specification findOne(long id) {
        //根据ID 查询出 规格主表数据
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        //根据规格ID  查询出  规格对应的选项数据
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);
        //封装到 组合实体
        return new Specification(tbSpecification,tbSpecificationOptions);
    }

    @Override
    public void update(Specification specification) {
        TbSpecification spec = specification.getSpec();
        List<TbSpecificationOption> specOptionList = specification.getSpecOptionList();
        // 1.更新spec信息
        tbSpecificationMapper.updateByPrimaryKey(spec);
        // 2.根据specId 进行条件删除
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(spec.getId());
        tbSpecificationOptionMapper.deleteByExample(example);
        // 3.在添加 规格选项集合
        if(specOptionList!=null&&specOptionList.size()>0){
            for(TbSpecificationOption tbSpecificationOption : specOptionList){
                tbSpecificationOption.setSpecId(spec.getId());
                tbSpecificationOptionMapper.insert(tbSpecificationOption);
            }
        }
    }

    @Override
    public void dele(Long[] ids) {
        if(ids!=null&&ids.length>0){
            for(Long id : ids){
                tbSpecificationMapper.deleteByPrimaryKey(id);
                TbSpecificationOptionExample example = new TbSpecificationOptionExample();
                example.createCriteria().andSpecIdEqualTo(id);
                tbSpecificationOptionMapper.deleteByExample(example);
            }
        }
    }

    @Override
    public List<Map> findSpecList() {
        return tbSpecificationMapper.findSpecList();
    }
}
